<?php
/*---------------------------------------------------------------------------
 小微OA系统 - 让工作更轻松快乐

 Copyright (c) 2013 http://www.smeoa.com All rights reserved.

 Author:  jinzhu.yin<smeoa@qq.com>

 Support: https://git.oschina.net/smeoa/xiaowei
 -------------------------------------------------------------------------*/

// 角色模型
namespace Home\Model;
use Think\Model;

class  DutyModel extends CommonModel {
    public $_validate = array( array('name', 'require', '名称必须'), );

    function get_duty_list($user_id) {
        $where['user_id'] = array('eq', $user_id);
        return M('DutyUser') -> where($where) -> select();
    }

    function del_duty($user_list) {
        if (empty($user_list)) {
            return true;
        }
        if (is_array($user_list)) {
            $user_list = array_filter($user_list);
        } else {
            $user_list = explode(",", $user_list);
            $user_list = array_filter($user_list);
        }
        $user_list = implode(",", $user_list);

        if (empty($user_list)) {
            return true;
        }
        $table = $this -> tablePrefix . 'duty_user';

        $result = $this -> db -> execute('delete from ' . $table . ' where user_id in (' . $user_list . ')');
        if ($result === false) {
            return false;
        } else {
            return true;
        }
    }

    function set_duty($user_list, $duty_list) {
        if (empty($user_list)) {
            return true;
        }
        //dump($role_id);
        if (is_array($user_list)) {
            $user_list = array_filter($user_list);
        } else {
            $user_list = array_filter(explode(",", $user_list));
        }
        $user_list = implode(",", $user_list);

        if (empty($user_list)) {
            return true;
        }
        if (is_array($duty_list)) {
            $duty_list = array_filter($duty_list);
        } else {
            $duty_list = array_filter(explode(",", $duty_list));
        }
        $duty_list = implode(",", $duty_list);

        $where = 'a.id in(' . $user_list . ') AND b.id in(' . $duty_list . ')';
        $sql = 'INSERT INTO ' . $this -> tablePrefix . 'duty_user (user_id,duty_id)';
        $sql .= ' SELECT a.id, b.id FROM ' . $this -> tablePrefix . 'user a, ' . $this -> tablePrefix . 'duty b WHERE ' . $where;
        $result = $this -> execute($sql);
        return result;
    }

    function get_auth($module_name, $user_id = null) {
        if (empty($user_id)) {
            $user_id = get_user_id();
        }
        $access_list = D("Node") -> access_list($user_id);
        $access_list = array_filter($access_list, array($this, 'filter_module'));
        $access_list = rotate($access_list);

        $module_list = $access_list['url'];
        $module_list = array_map(array($this, "get_module"), $module_list);
        $module_list = str_replace("_", "", $module_list);

        $access_list_admin = array_filter(array_combine($module_list, $access_list['admin']));
        $access_list_write = array_filter(array_combine($module_list, $access_list['write']));
        $access_list_read = array_filter(array_combine($module_list, $access_list['read']));

        $auth['admin'] = array_key_exists($module_name, $access_list_admin) || array_key_exists("##" . $module_name, $access_list_admin);

        $auth['write'] = array_key_exists($module_name, $access_list_write) || array_key_exists("##" . $module_name, $access_list_write);

        $auth['read'] = array_key_exists($module_name, $access_list_read) || array_key_exists("##" . $module_name, $access_list_read);

        if ($auth['admin'] == true) {
            $auth['write'] = true;
        }
        if ($auth['write'] == true) {
            $auth['read'] = true;
        }
        return $auth;
    }

    function get_module($str) {
        $arr_str = explode("/", $str);
        return $arr_str[0];
    }

    function filter_module($str) {
        if (strpos($str['url'], '##') !== false) {
            return true;
        }
        if (empty($str['admin']) && empty($str['write']) && empty($str['read'])) {
            return false;
        }
        if (strpos($str['url'], 'index')) {
            return true;
        }
        return false;
    }

    function check_duty($duty_no, $user_id = null) {
        if (empty($user_id)) {
            $user_id = get_user_id();
        }

        $role_list = $this -> get_role_list($user_id);
        $role_list = rotate($role_list);
        $role_list = $role_list['role_id'];

        $duty_list = $this -> get_duty_list($role_list);
        $duty_list = rotate($duty_list);
        $duty_list = $duty_list['duty_id'];

        $where_duty_id['duty_no'] = array('eq', $duty_no);
        $show_log_duty_id = M("Duty") -> where($where_duty_id) -> getField('id');

        if (in_array($show_log_duty_id, $duty_list)) {
            return true;
        } else {
            return false;
        }
    }

    function get_sub_duty_user_list($user_id) {
        $where1['user_id'] = array('eq', $user_id);
        $model_duty_user = M('DutyUser');
        $duty_list = $model_duty_user -> where($where1) -> getField('duty_id id,duty_id');

        if (!empty($duty_list)) {
            $where2['duty_id'] = array('in', $duty_list);
            $user_list = $model_duty_user -> where($where2) -> getField('user_id id,user_id');

            $where3['user_id'] = array('in', $user_list);
            $user_duty = $model_duty_user -> where($where3) -> getField('duty_id id,duty_id');

            $diff_duty = array_diff($user_duty, $duty_list);
            if (!empty($diff_duty)) {
                $where4['duty_id'] = array('in', $diff_duty);
                $diff_user = $model_duty_user -> where($where4) -> getField('user_id', TRUE);
                return array_diff($user_list, $diff_user);
            } else {
                return $user_list;
            }
        } else {
            $all_user = M('User') -> getField('id', TRUE);
            $duty_user = $model_duty_user -> getField('user_id', TRUE);

            return array_diff($all_user, $duty_user);
        }
    }

}
?>